package com.city.ajax.colligate;


import com.city.config.Code;
import com.city.domain.pub.PubFilepath;
import com.city.domain.report.CtBack;
import com.city.domain.report.CtDoBan;
import com.city.domain.report.CtReport;
import com.city.domain.report.CtTask;
import com.city.helper.AuthHelper;
import com.city.helper.EntityHelper;
import com.city.service.pub.PubFilepathService;
import com.city.service.report.CtTaskService;
import com.city.service.report.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.transaction.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @deprecation: 社会民意受理接口
 * @email: leichuqiao@126.com
 * @author: 雷楚桥
 * @date: 2018/7/27 16:10
 */
@RestController
@RequestMapping("/ajax/colligate")
public class ColligateAjax {

    @Autowired
    private ReportService reportService;
    @Autowired
    private EntityHelper entityHelper;
    @Autowired
    private AuthHelper authHelper;
    @Autowired
    private PubFilepathService filepathService;
    @Autowired
    private CtTaskService ctTaskService;


    /**
     * @deprecation 修改上报案件进度
     * @param  id Integer 保存id
     * @param  schedule Integer 案件进度
     * @return Map
     * @author 雷楚桥
     * @date 2018/7/27 16:11
     */
    @ResponseBody
    @RequestMapping("/reportScheduleChange")
    @Transactional
    public Map<String,Object> reportScheduleChange( @Param("id") Integer id,
                                                    @Param("schedule") Integer schedule,
                                                    @Param("endTime") String endTime){
        Map<String,Object> map = new HashMap<>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        //保存当前时间为受理时间
        CtTask ctTask = new CtTask();
        ctTask.setAgreeTime(df.format(new Date()));// new Date()为获取当前系统时间

        ctTask.setTaskId(id);
        ctTaskService.save(ctTask);
        //将受理时间保存

        reportService.updateEndTime(endTime,id);
        map.put("code", Code.SUCCESS);
        map.put("data", reportService.saveCtReport(id,schedule));
        return map;
    }

    /**
     * @deprecation 自行处置案件
     * @param id Integer 案件id
     * @return Map
     * @author 雷楚桥
     * @date 2018/7/29 12:32
     */
    @ResponseBody
    @RequestMapping("/selfDo")
    public Map<String,Object>  selfDo(@Param("id") Integer id){
        Map<String,Object> map = new HashMap<>();
        reportService.reportDispatchBranch(id,0,2);
        map.put("code", Code.SUCCESS);
        return map;
    }

    /**
     * @deprecation 获取所有的专业部门
     * @param parent Integer 专业部门的上级类别
     * @return Map<String,Object>
     * @author 雷楚桥
     * @date 2018/7/29 14:07
     */
    @ResponseBody
    @RequestMapping("/getBranch")
    public Map<String,Object> getBranch(@Param("parent") Integer parent){
        Map<String,Object> map = new HashMap<>();
        System.out.println(parent+"===========================");
        map.put("code", Code.SUCCESS);
        map.put("branchList",reportService.findAll());
        return map;
    }

    /**
     * @deprecation 案件派遣部门
     * @param reportId Integer 案件id
     * @param branchId Integer 专业部门id
     * @return Map<String,Object>
     * @author 雷楚桥
     * @date 2018/7/29 15:05
     */
    @ResponseBody
    @RequestMapping("/dispatchBranch")
    public Map<String,Object> dispatchBranch(@Param("reportId") Integer reportId,
                                             @Param("branchId") Integer branchId){
        Map<String,Object> map = new HashMap<>();
        System.out.println(reportId+"======"+branchId);
        reportService.reportDispatchBranch(reportId,branchId,2);
        map.put("code", Code.SUCCESS);
        return map;
    }

    /**
     * @deprecation 上报案件删除
     * @param id Integer 案件id
     * @return Map<String,Object>
     * @author 雷楚桥
     * @date 2018/7/29 17:08
     */
    @ResponseBody
    @RequestMapping("/reportDel")
    public Map<String,Object> reportDel(@Param("id") Integer id){
        Map<String,Object> map = new HashMap<>();
        reportService.reportDelById(id);
        map.put("code", Code.SUCCESS);
        return map;
    }

    /**
     * @deprecation 设置督办
     * @param id Integer 案件id
     * @return Map
     * @author 雷楚桥
     * @date 2018/7/30 10:49
     */
    @ResponseBody
    @RequestMapping("/dobanControl")
    public Map<String,Object> dobanControl(@Param("id") Integer id){
        Map<String,Object> map = new HashMap<>();
        CtDoBan doBan = entityHelper.createDoBan(id);
        doBan = reportService.saveDoBan(doBan);

        map.put("code", Code.SUCCESS);
        map.put("data",doBan);
        return map;
    }

    /**
     * @deprecation 取消督办
     * @param id Integer 案件id
     * @return Map
     * @author 雷楚桥
     * @date 2018/7/30 10:56
     */

    @ResponseBody
    @RequestMapping("/dobanCancel")
    public Map<String,Object> dobanCancel(@Param("id") Integer id){
        Map<String,Object> map = new HashMap<>();
        CtReport ctReport = reportService.cancelDoBan(id);
        map.put("code", Code.SUCCESS);
        map.put("data",ctReport);
        return map;
    }
    
    /**
     * @deprecation 指示灯预警[检查是否有新的并没有被人，需要提示]
     * @return Map
     * @author 雷楚桥
     * @date 2018-08-14 14:51
     */
    @ResponseBody
    @RequestMapping("/checkLight")
    public Map<String,Object> checkLight(){
        Map<String,Object> map = new HashMap<>();
        List<CtDoBan> doBanList = reportService.findAllDoBanByLightEqZero();
        if(doBanList.size() != 0){
            map.put("code", Code.LIGHT);//存在指示灯预警
            map.put("data",doBanList);
        }else
            map.put("code", Code.NO_LIGHT);//没有指示灯预警
        return map;
    }

    /**
     * @deprecation 核查结案[同意]
     * @param id Integer 案件id
     * @return Map
     * @author 雷楚桥
     * @date 2018-08-22 15:05
     */
    @ResponseBody
    @RequestMapping("/checkAccept")
    public Map<String,Object> checkAccept(@Param("id") Integer id){
        Map<String,Object> map = new HashMap<>();

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        //保存当前时间为完成时间

        ctTaskService.updateFinishTime(sf.format(new Date()),id);
        //将案件设为已结案，并且设置结案人
        reportService.checkAccepted(id,authHelper.thisUserId(),6);
        map.put("code", Code.SUCCESS);
        return map;
    }
    
    /**
     * @deprecation 案卷回撤
     * @param id Integer 案件id
     * @param remark String 备注
     * @return Map
     * @author 雷楚桥
     * @date 2018-08-22 16:07
     */
    @ResponseBody
    @RequestMapping("/reportBack")
    public Map<String,Object> reportBack(@Param("id") Integer id,
                                         @Param("remark") String remark){
        Map<String,Object> map = new HashMap<>();
        reportService.saveCtReport(id,2);                   //将这个案件设置为已派遣

        CtBack back = entityHelper.createBack(id,authHelper.thisUserId(),remark);

        reportService.saveBack(back);                               //案卷回撤记录

        map.put("code", Code.SUCCESS);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/9/6
     * @description  拿到文件列表
     */
    @PostMapping("/fileList")
    @ResponseBody
    public Map<String,Object> fileList(@Param("taskId") String taskId,
                                       @Param("type") Integer type){
        Map<String,Object> map = new HashMap<>();
        //上报文件
        if(type == null){
            List<PubFilepath> filepathList = filepathService.findListByTaskId(taskId);
            map.put("code",0);
            map.put("data",filepathList);
        }else if(type == 2){
            List<PubFilepath> filepathList2 = filepathService.findByTaskIdAndRenwu(taskId);
            map.put("code",0);
            map.put("data",filepathList2);
        }



        return map;
    }

    /**
     * @author  王宇
     * @date 2018/9/9
     * @description  派遣人员处置
     */
    @ResponseBody
    @RequestMapping("/dispatchPeople")
    public Map<String,Object> dispatchPeople(@Param("reportId") Integer reportId,
                                             @Param("peopleId") Integer peopleId){
        Map<String,Object> map = new HashMap<>();
        System.out.println(reportId+"======"+peopleId);

        reportService.dispatchPeople(reportId,peopleId,3);
        map.put("code", Code.SUCCESS);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/9/9
     * @description  通过审核
     */
    @PostMapping("/checkPass")
    @ResponseBody
    public Map<String,Object> checkPass(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        //保存当前时间为审核时间
        ctTaskService.updateCheckTime(df.format(new Date()),id);

        reportService.checkPass(id,authHelper.thisUserId(),5);//将案件设置为审核中


        map.put("code",Code.SUCCESS);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/9/9
     * @description  返工
     */
    @PostMapping("/passFailed")
    @ResponseBody
    public Map<String,Object> passFailed(@Param("id") Integer id,
                                         @Param("taskId") String taskId,
                                         @Param("comment") String comment){
        Map<String,Object> map = new HashMap<>();
        //通过任务id 删除 文件列表中 关于完成任务的文件,并且添加返工评论
        System.out.println(id);
        filepathService.deleteByTaskId(taskId);
        ctTaskService.updateReworkComment(comment,id);
        //返工操作  将 处置状态置为处理中 将激活返工状态
        reportService.reWork(id,1,3);
        map.put("code",Code.SUCCESS);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/9/10
     * @description  完成任务
     */
    @ResponseBody
    @RequestMapping("/reportFinish")
    public Map<String,Object> reportFinish( @Param("id") Integer id,
                                            @Param("schedule") Integer schedule){
        Map<String,Object> map = new HashMap<>();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        //保存当前时间为完成时间

        ctTaskService.updateWorkTime(sf.format(new Date()),id);
        CtReport ctReport = reportService.findReportById(id);
        try {
            Date d1 =  sf.parse(ctReport.getDeadline());
            //如果比较结果为真，就再规定期限内完成，将状态变成 按期完成
            if(new Date().getTime() < d1.getTime()){
                ctReport.setIsontime(1);
                ctReport = reportService.saveCtReport(ctReport);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }


        //将受理时间保存
        map.put("code", Code.SUCCESS);
        map.put("data", reportService.saveCtReport(id,schedule));
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/9/18
     * @description  案件回撤
     */
    @PostMapping("/caseback")
    @ResponseBody
    public Map<String,Object> caseBack(@Param("id") Integer id,
                                       @Param("taskId") String taskId,
                                       @Param("comment") String comment){
        Map<String,Object> map = new HashMap<>();

        //将状态切换，并且添上回撤属性

        reportService.caseBack(id,1,1);
        ctTaskService.updateCaseComment(comment,id);

        map.put("code",Code.SUCCESS);
        return map;
    }

}
